package com.termine.jsf;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.ListDataModel;

import org.primefaces.model.SelectableDataModel;

import com.termine.bean.Bean;
import com.termine.entity.Service;
import com.termine.util.Logger;

@ManagedBean
@SessionScoped
public class ServiceJSF extends Bean implements Serializable {

	private static final long serialVersionUID = 4823513814475319504L;

	private ServiceDataModel serviceDataModel;
	private Service selectedService;
	private Service newService;

	/*
	 * Init
	 */
	@PostConstruct
	public void init() {
		try {
			serviceDataModel = new ServiceDataModel(getServiceDAO()
					.getAllServicesForProvider(getLoginJSF().getPersonalData()));
			newService = new Service();
		} catch (Exception e) {
			Logger.log(e.getMessage());
			e.printStackTrace();
		}
	}

	/*
	 * Jsf Action Methods
	 */
	public String addService() {
		try {
			getServiceDAO().addService(newService,
					getLoginJSF().getPersonalData().getProvider_id());
			init();
			return "SUCCESS";
		} catch (Exception e) {
			Logger.log(e.getMessage());
			e.printStackTrace();
			return "ERROR";
		}
	}

	public String deleteService() {
		try {
			getServiceDAO().deleteService(selectedService);
			init();
			return "SUCCESS";
		} catch (Exception e) {
			Logger.log(e.getMessage());
			e.printStackTrace();
			return "ERROR";
		}
	}

	public String updateService() {
		try {
			getServiceDAO().updateService(selectedService);
			init();
			return "SUCCESS";
		} catch (Exception e) {
			Logger.log(e.getMessage());
			e.printStackTrace();
			return "ERROR";
		}
	}

	/*
	 * TableModel
	 */
	public class ServiceDataModel extends ListDataModel<Service> implements
			SelectableDataModel<Service> {

		public ServiceDataModel() {
		}

		public ServiceDataModel(List<Service> data) {
			super(data);
		}

		@Override
		public Service getRowData(String rowKey) {
			@SuppressWarnings("unchecked")
			List<Service> services = (List<Service>) getWrappedData();

			for (Service service : services) {
				if (service.getService_id().equals(Integer.parseInt(rowKey))) {
					return service;
				}
			}
			return null;
		}

		@Override
		public Object getRowKey(Service service) {
			return service.getService_id();
		}
	}

	/*
	 * Getters & Setters
	 */
	public ServiceDataModel getServiceDataModel() {
		return serviceDataModel;
	}

	public void setServiceDataModel(ServiceDataModel serviceDataModel) {
		this.serviceDataModel = serviceDataModel;
	}

	public Service getSelectedService() {
		return selectedService;
	}

	public void setSelectedService(Service selectedService) {
		this.selectedService = selectedService;
	}

	public Service getNewService() {
		return newService;
	}

	public void setNewService(Service newService) {
		this.newService = newService;
	}

}
